﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.MaterialLists;

namespace SmartProduction.Write.API.Application.CommandHandlers.MaterialLists
{
    /// <summary>
    /// 物料类型命令处理添加
    /// </summary>
    public class MaterListTypeCreateCommandHandler:IRequestHandler<MaterListTypeCreateCommand,APIResult<object>>
    {
        /// <summary>
        /// 物料类型
        /// </summary>
        private readonly IBaseRepository<MaterialListType> typeRep;
        /// <summary>
        /// mapper映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="typeRep">物料类型</param>
        /// <param name="mapper">mapper映射</param>
        public MaterListTypeCreateCommandHandler(IBaseRepository<MaterialListType> typeRep,IMapper mapper)
        {
            this.typeRep = typeRep;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">实体</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回的任务</returns>
        public async Task<APIResult<object>> Handle(MaterListTypeCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity = mapper.Map<MaterialListType>(request);
            var r=typeRep.AddAsync(entity).Result;

            if (r > 0)
            {
                result.Code = APIEnums.添加成功;
                result.Msg = APIEnums.添加成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnums.添加失败;
                result.Msg = APIEnums.添加失败.ToString();
                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
